using System;
using log4net;
using mojoPortal.Business;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using Resources;
using ud_Forum.Business;

namespace ud_Forum
{
    public partial class UnsubscribeForumThread : mojoBasePage
	{
		// Create a logger for use in this class
		private static readonly ILog log = LogManager.GetLogger(typeof(UnsubscribeForumThread));

        #region OnInit
        override protected void OnInit(EventArgs e)
        {
            Load += Page_Load;
            base.OnInit(e);
        }
        #endregion


        private void Page_Load(object sender, EventArgs e)
		{
            Title = SiteUtils.FormatPageTitle(siteSettings, ForumResources.UnSubscribeLink);
            
			if (!Request.IsAuthenticated)
			{
				lblUnsubscribe.Text = ResourceHelper.GetMessageTemplate("AccessDeniedMessage.config");
				return;
			}

            int threadID = WebUtils.ParseInt32FromQueryString("threadid", -1);

            if (threadID > -1)
            {
                UnsubscribeUser(threadID);
            }

            AddClassToBody("forumthreadunsubscribe");
					
		}

        private void UnsubscribeUser(int threadId)
        {
            SiteUser siteUser = SiteUtils.GetCurrentSiteUser();
            if (siteUser == null) return;
            if (!ForumThread.Unsubscribe(threadId, siteUser.UserId))
            {
                log.ErrorFormat("ForumThread.UnSubscribe({0}, {1}) failed", threadId, siteUser.UserId);
                lblUnsubscribe.Text = ForumResources.ForumThreadUnsubscribeFailed;
                return;
            }
            lblUnsubscribe.Text = ForumResources.ForumThreadUnsubscribeCompleted;	

        }


		
	}
}
